package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.EnteringStudent;
import com.xmy.cultivate.entity.views.ScoreData;
import com.xmy.cultivate.entity.views.ScoreDataDetail;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 成绩管理 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-04-14
 */
@RestController
@RequestMapping("/api/transcript")
public class TranscriptController extends BaseController {

    @Autowired
    ITranscriptService iTranscriptService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    ITranscriptSafetyScoreSetService iTranscriptSafetyScoreSetService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    ITranscriptSafetyDiffSetService iTranscriptSafetyDiffSetService;


    /**
     * @return
     */
    @PostMapping("/saveScore")
    public Result saveScore(@RequestBody EnteringStudent enteringStudent) {

        List<Transcript> transcriptList = enteringStudent.getTranscriptList();
        for (Transcript transcript : transcriptList) {
            iTranscriptService.save(transcript);
        }
        return new Result(Code.OK, "", "ok");
    }

    /**
     * 成绩数据
     *
     * @return
     */
    @GetMapping("/scoreDataList")
    public Result scoreDataList(
            @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
            @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
            @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
            @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum,
            @RequestParam(value = "examNum", defaultValue = "") String examNum,
            @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
            @RequestParam(value = "include", defaultValue = "1") String include,
            @RequestParam(value = "type", defaultValue = "") String type,
            @RequestParam(value = "classify", defaultValue = "") String classify,
            @RequestParam(value = "trackType", defaultValue = "") String trackType,
            @RequestParam(value = "teamId", defaultValue = "") String teamId,
            @RequestParam(value = "courseTypeId", defaultValue = "") String courseTypeId,
            @RequestParam(value = "gradeSection", defaultValue = "") String gradeSection) {

        if (StringUtils.isEmpty(yearPart)) {
            return new Result(Code.ERROR, "", "年份不能为空");
        } else if (StringUtils.isEmpty(quarterNum)) {
            return new Result(Code.ERROR, "", "学期不能为空");
        } else if (StringUtils.isEmpty(examNum)) {
            return new Result(Code.ERROR, "", "考试名称不能为空");
        } else if (StringUtils.isEmpty(type)) {
            return new Result(Code.ERROR, "", "排行不能为空");
        }

        StringBuffer yearClassIdSb = new StringBuffer();
        if (gradeSection != null && !gradeSection.equals("")) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            yearClassId = yearClassIdSb.toString();
        }

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
        transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
        safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");
        //safetyDiffSetQueryWrapper.apply(StringUtils.isNotBlank(yearClassId),"FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }
        List<ScoreData> scoreDataList = new ArrayList<>();

        if (type.equals("5")) {
            scoreDataList = iTranscriptService.scoreTeamData(subjectsId, yearPart, quarterNum, examNum, yearClassId, include, courseTypeId, classify, trackType, teamId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList);
        } else {

            if (StringUtils.isBlank(schoolId)) {
                if (type.equals("2")) {
                    //校区老师状态，只能看自己所在校区的数据
                    schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
                }
            }

            scoreDataList = iTranscriptService.scoreData(schoolId, subjectsId, yearPart, quarterNum, examNum, yearClassId, type, include, courseTypeId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList);

            if (type.equals("6") && StringUtils.isBlank(schoolId)) {
                List<ScoreData> collectList = new ArrayList<>();

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("year_part", yearPart);
                queryWrapper.eq("quarter_num", quarterNum);
                queryWrapper.eq("pid", 0);
                List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

                for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                    ScoreData scoreData = new ScoreData();
                    scoreData.setSchoolId(schoolQuarter.getSchoolId());
                    scoreData.setSchoolName(schoolQuarter.getSchoolName());
                    scoreData.setSchoolPid(schoolQuarter.getPid());
                    scoreData.setStudentAllCount(0);
                    scoreData.setStuRealityCount(0);
                    scoreData.setNotCounted(0);
                    scoreData.setNotCalcCount(0);
                    scoreData.setCarryCount(0);
                    scoreData.setNoPassCount(0);
                    scoreData.setPassCount(0);
                    scoreData.setRetrogressCount(0);
                    scoreData.setNotCalcPassCount(0);
                    scoreData.setNoPassRate(new BigDecimal("0.00"));
                    scoreData.setPassRate(new BigDecimal("0.00"));
                    scoreData.setCarryRate(new BigDecimal("0.00"));
                    scoreData.setSecureRate(new BigDecimal("0.00"));

                    collectList.add(scoreData);
                }

                for (ScoreData scoreData : collectList) {
                    List<ScoreData> childRates = new ArrayList<>();
                    for (ScoreData data : scoreDataList) {
                        if (scoreData.getSchoolId().equals(data.getSchoolId()) || scoreData.getSchoolId().equals(data.getSchoolPid())) {
                            childRates.add(data);
                            if (data.getStudentAllCount() != null) {
                                scoreData.setStudentAllCount(scoreData.getStudentAllCount() + data.getStudentAllCount());
                            }
                            if (data.getStuRealityCount() != null) {
                                scoreData.setStuRealityCount(scoreData.getStuRealityCount() + data.getStuRealityCount());
                            }
                            if (data.getNotCounted() != null) {
                                scoreData.setNotCounted(scoreData.getNotCounted() + data.getNotCounted());
                            }
                            if (data.getNotCalcCount() != null) {
                                scoreData.setNotCalcCount(scoreData.getNotCalcCount() + data.getNotCalcCount());
                            }
                            if (data.getCarryCount() != null) {
                                scoreData.setCarryCount(scoreData.getCarryCount() + data.getCarryCount());
                            }
                            if (data.getNoPassCount() != null) {
                                scoreData.setNoPassCount(scoreData.getNoPassCount() + data.getNoPassCount());
                            }
                            if (data.getPassCount() != null) {
                                scoreData.setPassCount(scoreData.getPassCount() + data.getPassCount());
                            }
                            if (data.getRetrogressCount() != null) {
                                scoreData.setRetrogressCount(scoreData.getRetrogressCount() + (data.getRetrogressCount()));
                            }
                            if (data.getNotCalcPassCount() != null) {
                                scoreData.setNotCalcPassCount(scoreData.getNotCalcPassCount() + (data.getNotCalcPassCount()));
                            }

                            if (data.getNoPassRate() != null) {
                                scoreData.setNoPassRate(scoreData.getNoPassRate().add(data.getNoPassRate()));
                            }
                            if (data.getPassRate() != null) {
                                scoreData.setPassRate(scoreData.getPassRate().add(data.getPassRate()));
                            }
                            if (data.getCarryRate() != null) {
                                scoreData.setCarryRate(scoreData.getCarryRate().add(data.getCarryRate()));
                            }
                            if (data.getSecureRate() != null) {
                                scoreData.setSecureRate(scoreData.getSecureRate().add(data.getSecureRate()));
                            }
                        }
                    }
                    scoreData.setChildRateList(childRates);
                }
                scoreDataList = collectList;
            }

        }
        return new Result(Code.OK, new PageResult<>(scoreDataList, (long) scoreDataList.size(), 1L), "");
    }

    @GetMapping("/scoreDataListIFeign")
    public List<ScoreData> scoreDataListIFeign(
            @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
            @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
            @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
            @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum,
            @RequestParam(value = "examNum", defaultValue = "") String examNum,
            @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
            @RequestParam(value = "include", defaultValue = "1") String include,
            @RequestParam(value = "type", defaultValue = "") String type,
            @RequestParam(value = "classify", defaultValue = "") String classify,
            @RequestParam(value = "trackType", defaultValue = "") String trackType,
            @RequestParam(value = "teamId", defaultValue = "") String teamId,
            @RequestParam(value = "courseTypeId", defaultValue = "") String courseTypeId,
            @RequestParam(value = "gradeSection", defaultValue = "") String gradeSection) {

        StringBuffer yearClassIdSb = new StringBuffer();
        if (gradeSection != null && !gradeSection.equals("")) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            yearClassId = yearClassIdSb.toString();
        }

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
        transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
        safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");
        //safetyDiffSetQueryWrapper.apply(StringUtils.isNotBlank(yearClassId),"FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }
        List<ScoreData> scoreDataList = new ArrayList<>();

        if (type.equals("5")) {
            scoreDataList = iTranscriptService.scoreTeamData(subjectsId, yearPart, quarterNum, examNum, yearClassId, include, courseTypeId, classify, trackType, teamId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList);
        } else {

            if (StringUtils.isBlank(schoolId)) {
                if (type.equals("2")) {
                    //校区老师状态，只能看自己所在校区的数据
                    schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
                }
            }

            scoreDataList = iTranscriptService.scoreData(schoolId, subjectsId, yearPart, quarterNum, examNum, yearClassId, type, include, courseTypeId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList);

            if (type.equals("6") && StringUtils.isBlank(schoolId)) {
                List<ScoreData> collectList = new ArrayList<>();

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("year_part", yearPart);
                queryWrapper.eq("quarter_num", quarterNum);
                queryWrapper.eq("pid", 0);
                List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

                for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                    ScoreData scoreData = new ScoreData();
                    scoreData.setSchoolId(schoolQuarter.getSchoolId());
                    scoreData.setSchoolName(schoolQuarter.getSchoolName());
                    scoreData.setSchoolPid(schoolQuarter.getPid());
                    scoreData.setStudentAllCount(0);
                    scoreData.setStuRealityCount(0);
                    scoreData.setNotCounted(0);
                    scoreData.setNotCalcCount(0);
                    scoreData.setCarryCount(0);
                    scoreData.setNoPassCount(0);
                    scoreData.setPassCount(0);
                    scoreData.setRetrogressCount(0);
                    scoreData.setNotCalcPassCount(0);
                    scoreData.setNoPassRate(new BigDecimal("0.00"));
                    scoreData.setPassRate(new BigDecimal("0.00"));
                    scoreData.setCarryRate(new BigDecimal("0.00"));
                    scoreData.setSecureRate(new BigDecimal("0.00"));

                    collectList.add(scoreData);
                }

                for (ScoreData scoreData : collectList) {
                    List<ScoreData> childRates = new ArrayList<>();
                    for (ScoreData data : scoreDataList) {
                        if (scoreData.getSchoolId().equals(data.getSchoolId()) || scoreData.getSchoolId().equals(data.getSchoolPid())) {
                            childRates.add(data);
                            if (data.getStudentAllCount() != null) {
                                scoreData.setStudentAllCount(scoreData.getStudentAllCount() + data.getStudentAllCount());
                            }
                            if (data.getStuRealityCount() != null) {
                                scoreData.setStuRealityCount(scoreData.getStuRealityCount() + data.getStuRealityCount());
                            }
                            if (data.getNotCounted() != null) {
                                scoreData.setNotCounted(scoreData.getNotCounted() + data.getNotCounted());
                            }
                            if (data.getNotCalcCount() != null) {
                                scoreData.setNotCalcCount(scoreData.getNotCalcCount() + data.getNotCalcCount());
                            }
                            if (data.getCarryCount() != null) {
                                scoreData.setCarryCount(scoreData.getCarryCount() + data.getCarryCount());
                            }
                            if (data.getNoPassCount() != null) {
                                scoreData.setNoPassCount(scoreData.getNoPassCount() + data.getNoPassCount());
                            }
                            if (data.getPassCount() != null) {
                                scoreData.setPassCount(scoreData.getPassCount() + data.getPassCount());
                            }
                            if (data.getRetrogressCount() != null) {
                                scoreData.setRetrogressCount(scoreData.getRetrogressCount() + (data.getRetrogressCount()));
                            }
                            if (data.getNotCalcPassCount() != null) {
                                scoreData.setNotCalcPassCount(scoreData.getNotCalcPassCount() + (data.getNotCalcPassCount()));
                            }

                            if (data.getNoPassRate() != null) {
                                scoreData.setNoPassRate(scoreData.getNoPassRate().add(data.getNoPassRate()));
                            }
                            if (data.getPassRate() != null) {
                                scoreData.setPassRate(scoreData.getPassRate().add(data.getPassRate()));
                            }
                            if (data.getCarryRate() != null) {
                                scoreData.setCarryRate(scoreData.getCarryRate().add(data.getCarryRate()));
                            }
                            if (data.getSecureRate() != null) {
                                scoreData.setSecureRate(scoreData.getSecureRate().add(data.getSecureRate()));
                            }
                        }
                    }
                    scoreData.setChildRateList(childRates);
                }
                scoreDataList = collectList;
            }

        }
        return scoreDataList;
    }

    /**
     * 实际统计情况
     *
     * @return
     */
    @GetMapping("/scoreDataDetail")
    public Result scoreDataDetail(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "column", defaultValue = "") Integer column,// 1:本次成绩排序 2:之前成绩排序
            @RequestParam(value = "order", defaultValue = "") Integer order,// 1:升序 2:降序
            @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
            @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
            @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
            @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum,
            @RequestParam(value = "examNum", defaultValue = "") String examNum,
            @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
            @RequestParam(value = "type", defaultValue = "") String type,
            @RequestParam(value = "isStatistics", defaultValue = "") String isStatistics,
            @RequestParam(value = "gradeId", defaultValue = "") String gradeId,
            @RequestParam(value = "include", defaultValue = "1") String include,
            @RequestParam(value = "studentName", defaultValue = "") String studentName,
            @RequestParam(value = "courseTypeId", defaultValue = "") String courseTypeId,
            @RequestParam(value = "teacherId", defaultValue = "") String teacherId,
            @RequestParam(value = "classify", defaultValue = "") String classify,
            @RequestParam(value = "trackType", defaultValue = "") String trackType,
            @RequestParam(value = "teamId", defaultValue = "") String teamId,
            @RequestParam(value = "gradeSection", defaultValue = "") String gradeSection) {

        if (StringUtils.isEmpty(yearPart)) {
            return new Result(Code.ERROR, "", "年份不能为空");
        } else if (StringUtils.isEmpty(quarterNum)) {
            return new Result(Code.ERROR, "", "学期不能为空");
        } else if (StringUtils.isEmpty(examNum)) {
            return new Result(Code.ERROR, "", "考试名称不能为空");
        } else if (StringUtils.isEmpty(type)) {
            return new Result(Code.ERROR, "", "排行不能为空");
        }

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
        transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
        safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }

        IPage<ScoreDataDetail> iPage = new Page<>(pageNum, pageSize);
        if (type.equals("5")) {
            iTranscriptService.scoreDataTeamDetail(iPage, schoolId, subjectsId, yearPart, quarterNum, examNum, gradeSection, yearClassId, type, teacherId, gradeId, isStatistics, include, studentName, courseTypeId, classify, trackType, teamId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, column, order);
        } else {
            iTranscriptService.scoreDataDetail(iPage, schoolId, subjectsId, yearPart, quarterNum, examNum, gradeSection, yearClassId, type, teacherId, gradeId, isStatistics, include, studentName, courseTypeId, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, column, order);
        }

        String examName = CommonUtil.getExamNameForId(Integer.parseInt(examNum));
        Map<String, Object> map = new HashMap<>();
        map.put("examNum", examNum);
        map.put("examName", examName);
        map.put("yearPart", Integer.parseInt(yearPart));
        map.put("quarterNum", Integer.parseInt(quarterNum));
        map.put("quarterName", CommonUtil.getQuarterNameForId(Integer.parseInt(quarterNum)));

        QueryWrapper<TranscriptSafetyDiffSet> upWrapper = new QueryWrapper<>();
        upWrapper.le("quarter_id_start", quarter.getId());
        upWrapper.ge("quarter_id_end", quarter.getId());
        upWrapper.eq("exam_num", examNum);

        StringBuilder quarterNumConditions = new StringBuilder();
        if (StringUtils.isNotBlank(quarterNum)) {
            List<String> labelIdList = Arrays.asList(quarterNum.split(","));
            for (String strItem : labelIdList) {
                if (quarterNumConditions.length() > 0) {
                    quarterNumConditions.append(" OR ");
                }
                quarterNumConditions.append("FIND_IN_SET('").append(strItem).append("', quarter_num_str)");
            }
            if (quarterNumConditions.length() > 0) {
                upWrapper.apply(" (" + quarterNumConditions.toString() + ") ");
            }
        }

        StringBuilder yearClassIdConditions = new StringBuilder();
        if (StringUtils.isNotBlank(yearClassId)) {
            List<String> labelIdList = Arrays.asList(yearClassId.split(","));
            for (String strItem : labelIdList) {
                if (yearClassIdConditions.length() > 0) {
                    yearClassIdConditions.append(" OR ");
                }
                yearClassIdConditions.append("FIND_IN_SET('").append(strItem).append("', year_class_id_str)");
            }
            if (yearClassIdConditions.length() > 0) {
                upWrapper.apply(" (" + yearClassIdConditions.toString() + ") ");
            }
        }

        String subjectsId2 = subjectsId;
        if (StringUtils.isBlank(subjectsId)) {
            subjectsId2 = "1,2,3";//没有的话查所有
        }

        StringBuilder subjectsIdConditions = new StringBuilder();
        if (StringUtils.isNotBlank(subjectsId2)) {
            List<String> labelIdList = Arrays.asList(subjectsId2.split(","));
            for (String strItem : labelIdList) {
                if (subjectsIdConditions.length() > 0) {
                    subjectsIdConditions.append(" OR ");
                }
                subjectsIdConditions.append("FIND_IN_SET('").append(strItem).append("', subjects_id_str)");
            }
            if (subjectsIdConditions.length() > 0) {
                upWrapper.apply(" (" + subjectsIdConditions.toString() + ") ");
            }
        }

        List<TranscriptSafetyDiffSet> tTranscriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(upWrapper);

        if (tTranscriptSafetyDiffSetList.size() == 1) {
            map.put("upType", 1);// 显示对比名字

            for (TranscriptSafetyDiffSet tTranscriptSafetyDiffSet : tTranscriptSafetyDiffSetList) {
                String upExamName = CommonUtil.getExamNameForId(tTranscriptSafetyDiffSet.getContrastExamNum());
                map.put("upExamName", upExamName);
                Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tTranscriptSafetyDiffSet.getContrastDiffNum());
                map.put("upExamNum", upQuarter.getSort());
                map.put("upYearPart", upQuarter.getYearPart());
                map.put("upQuarterName", upQuarter.getAliasName());
                map.put("upQuarterNum", upQuarter.getNum());
            }

        } else {
            map.put("upType", 0);// 显示对比分数
        }

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages(), map), "");
    }

    /**
     * 退步详情
     */
    @GetMapping("/otherScoreDataDetail")
    public Result otherScoreDataDetail(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "column", defaultValue = "") Integer column,// 1:本次成绩排序 2:之前成绩排序 3:退步排序
            @RequestParam(value = "order", defaultValue = "") Integer order,// 1:升序 2:降序
            @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
            @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
            @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
            @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum,
            @RequestParam(value = "examNum", defaultValue = "") String examNum,
            @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
            @RequestParam(value = "type", defaultValue = "") String type,
            @RequestParam(value = "gradeId", defaultValue = "") String gradeId,
            @RequestParam(value = "teacherId", defaultValue = "") String teacherId,
            @RequestParam(value = "gradeSection", defaultValue = "") String gradeSection,
            @RequestParam(value = "scoreDetailType", defaultValue = "") String scoreDetailType,
            @RequestParam(value = "include", defaultValue = "1") String include,
            @RequestParam(value = "studentName", defaultValue = "") String studentName,
            @RequestParam(value = "courseTypeId", defaultValue = "") String courseTypeId,
            @RequestParam(value = "status", defaultValue = "") String status,
            @RequestParam(value = "classify", defaultValue = "") String classify,
            @RequestParam(value = "trackType", defaultValue = "") String trackType,
            @RequestParam(value = "teamId", defaultValue = "") String teamId
    ) {
        if (StringUtils.isEmpty(yearPart)) {
            return new Result(Code.ERROR, "", "年份不能为空");
        } else if (StringUtils.isEmpty(quarterNum)) {
            return new Result(Code.ERROR, "", "学期不能为空");
        } else if (StringUtils.isEmpty("examNum")) {
            return new Result(Code.ERROR, "", "考试名称不能为空");
        } else if (StringUtils.isEmpty(type)) {
            return new Result(Code.ERROR, "", "排行不能为空");
        }

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        String examName = CommonUtil.getExamNameForId(Integer.parseInt(examNum));
        Map<String, Object> map = new HashMap<>();
        map.put("examNum", examNum);
        map.put("examName", examName);
        map.put("yearPart", Integer.parseInt(yearPart));
        map.put("quarterNum", Integer.parseInt(quarterNum));
        map.put("quarterName", CommonUtil.getQuarterNameForId(Integer.parseInt(quarterNum)));

        QueryWrapper<TranscriptSafetyDiffSet> upWrapper = new QueryWrapper<>();
        upWrapper.le("quarter_id_start", quarter.getId());
        upWrapper.ge("quarter_id_end", quarter.getId());
        upWrapper.eq("exam_num", examNum);

        StringBuilder quarterNumConditions = new StringBuilder();
        if (StringUtils.isNotBlank(quarterNum)) {
            List<String> labelIdList = Arrays.asList(quarterNum.split(","));
            for (String strItem : labelIdList) {
                if (quarterNumConditions.length() > 0) {
                    quarterNumConditions.append(" OR ");
                }
                quarterNumConditions.append("FIND_IN_SET('").append(strItem).append("', quarter_num_str)");
            }
            if (quarterNumConditions.length() > 0) {
                upWrapper.apply(" (" + quarterNumConditions.toString() + ") ");
            }
        }

        StringBuilder yearClassIdConditions = new StringBuilder();
        if (StringUtils.isNotBlank(yearClassId)) {
            List<String> labelIdList = Arrays.asList(yearClassId.split(","));
            for (String strItem : labelIdList) {
                if (yearClassIdConditions.length() > 0) {
                    yearClassIdConditions.append(" OR ");
                }
                yearClassIdConditions.append("FIND_IN_SET('").append(strItem).append("', year_class_id_str)");
            }
            if (yearClassIdConditions.length() > 0) {
                upWrapper.apply(" (" + yearClassIdConditions.toString() + ") ");
            }
        }

        String subjectsId2 = subjectsId;
        if (StringUtils.isBlank(subjectsId)) {
            subjectsId2 = "1,2,3";//没有的话查所有
        }

        StringBuilder subjectsIdConditions = new StringBuilder();
        if (StringUtils.isNotBlank(subjectsId2)) {
            List<String> labelIdList = Arrays.asList(subjectsId2.split(","));
            for (String strItem : labelIdList) {
                if (subjectsIdConditions.length() > 0) {
                    subjectsIdConditions.append(" OR ");
                }
                subjectsIdConditions.append("FIND_IN_SET('").append(strItem).append("', subjects_id_str)");
            }
            if (subjectsIdConditions.length() > 0) {
                upWrapper.apply(" (" + subjectsIdConditions.toString() + ") ");
            }
        }

        List<TranscriptSafetyDiffSet> tTranscriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(upWrapper);

        if (tTranscriptSafetyDiffSetList.size() == 1) {
            map.put("upType", 1);// 显示对比名字

            for (TranscriptSafetyDiffSet tTranscriptSafetyDiffSet : tTranscriptSafetyDiffSetList) {
                String upExamName = CommonUtil.getExamNameForId(tTranscriptSafetyDiffSet.getContrastExamNum());
                map.put("upExamName", upExamName);
                Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tTranscriptSafetyDiffSet.getContrastDiffNum());
                map.put("upExamNum", upQuarter.getSort());
                map.put("upYearPart", upQuarter.getYearPart());
                map.put("upQuarterName", upQuarter.getAliasName());
                map.put("upQuarterNum", upQuarter.getNum());
            }

        } else {
            map.put("upType", 0);// 显示对比分数
        }

        IPage<ScoreDataDetail> iPage = new Page<>(pageNum, pageSize);

        iTranscriptService.otherScoreDataDetail(iPage, schoolId, subjectsId, yearPart, quarterNum, examNum, gradeSection, yearClassId, type, teacherId, gradeId, scoreDetailType, status, include, studentName, courseTypeId, classify, trackType, teamId, column, order);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages(), map), "");
    }
}
